package cn.crm.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.ResultTransformer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.crm.bean.Customer;
import cn.crm.bean.DDL;
import cn.crm.dao.CustomerDao;
import cn.crm.dao.DDLDao;

@Repository("customerDao")
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	@Resource
	private DDLDao ddlDao; 
	
	@Override
	public List<Customer> getAll() {
		List<Customer> list = super.getHibernateTemplate().find(
				"from Customer where custIsdelete=?", false);
		return list.size() > 0 ? list : null;
	}

	@Override
	public Customer getCustomerById(Integer id) {
		List<Customer> list = super.getHibernateTemplate().find(
				"from Customer where custCid=?", id);
		return list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public Object save(Customer customer) {
		return super.getHibernateTemplate().save(customer);
	}

	@Override
	public void update(Customer customer) {
		super.getHibernateTemplate().update(customer);

	}

	@Override
	public void delete(Customer customer) {
		super.getHibernateTemplate().delete(customer);
	}

	@Override
	public List<Customer> getAll(Integer index, Integer count, Object... params) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		criteria.add(Restrictions.eq("custIsdelete", false));
		if (params != null) {
			for (Object res : params) {
				if (res != null) {
					System.out.println(res);
					criteria.add((Criterion) res);
				}
			}
		}
		return super.getHibernateTemplate().findByCriteria(criteria, index,
				count);
	}

	@Override
	public Object getCount(String hql) {
		Session session = super.getSession();
		Object count;
		Query query = session.createQuery(hql);
		count = query.uniqueResult();
		return count;
	}

	@Override
	public Object getCount(Object... params) {
		Session session = super.getSession();
		Criteria cri = session.createCriteria(Customer.class);
		cri.setProjection(Projections.rowCount());
	    cri.add(Restrictions.eq("custIsdelete", false));
		if (params != null) {
			for (Object res : params) {
				if (res != null) {
					System.out.println(res);
					cri.add((Criterion) res);
				}
			}
		}
		List list = cri.list();
		return list.size() > 0 ? list.get(0) : 0;
	}

	
	@Override
	public Integer getGroup(Integer id,String column) {
		Session session = super.getSession();
		Criteria cri = session.createCriteria(Customer.class);
		cri.setProjection(Projections.rowCount());
		cri.add(Restrictions.eq(column,id));
		List list = cri.list();
		return list.size()>0?Integer.parseInt(list.get(0).toString()):0;
	}

	public void setDdlDao(DDLDao ddlDao) {
		this.ddlDao = ddlDao;
	}

	
	
}
